package com.ciwong.epaper.modules.epaper.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.bookstore.bean.StudentGroupBean;
import com.ciwong.epaper.modules.clazz.bean.StudentBean;
import com.ciwong.epaper.modules.epaper.ui.ChooseGroupActivity;

import java.util.ArrayList;
import java.util.List;

public class ChooseGroupAdapter extends BaseExpandableListAdapter {

    private List<StudentGroupBean> studentBeans;

    private Context mContext;

    private String teamName;

    private int stuCount;

    private int checkCountAll;


    public ChooseGroupAdapter(List<StudentGroupBean> studentBeans, Context mContext){
        this.studentBeans = studentBeans;
        this.mContext = mContext;
    }

    @Override
    public int getGroupCount() {
        return null == studentBeans ? 0 : studentBeans.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return null == studentBeans ? 0 : studentBeans.get(i).getList().size();
    }

    @Override
    public Object getGroup(int i) {
        return null == studentBeans ? null : studentBeans.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return null == studentBeans ? null : studentBeans.get(i).getList().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        final ViewHolderGroup viewHolderGroup ;
        if (null == view){
            view = View.inflate(mContext, R.layout.activity_choose_group_par,null);
            viewHolderGroup = new ViewHolderGroup();
            viewHolderGroup.class_name_check = (CheckBox) view.findViewById(R.id.class_name_check);
            viewHolderGroup.class_name_text = (TextView) view.findViewById(R.id.class_name_text);
            viewHolderGroup.class_count_text = (TextView) view.findViewById(R.id.class_count_text);
            viewHolderGroup.class_name_go = (ImageView) view.findViewById(R.id.class_name_go);
            view.setTag(viewHolderGroup);
        }else {
            viewHolderGroup = (ViewHolderGroup) view.getTag();
        }

        if (null != studentBeans && studentBeans.size() > 0){
            int checkCount = studentBeans.get(i).getCheckCount();
            stuCount = studentBeans.get(i).getList().size();
            if (stuCount == checkCount){
                viewHolderGroup.class_name_check.setChecked(true);
                viewHolderGroup.class_name_check.setSelected(true);
            }else if (0 == checkCount){
                viewHolderGroup.class_name_check.setChecked(false);
                viewHolderGroup.class_name_check.setSelected(true);
            }else {
                viewHolderGroup.class_name_check.setChecked(true);
                viewHolderGroup.class_name_check.setSelected(false);
            }
            teamName = studentBeans.get(i).getTeamName();
            if ("".equals(teamName)){
                teamName = mContext.getString(R.string.not_group);
            }
            viewHolderGroup.class_name_text.setText(teamName);
            viewHolderGroup.class_count_text.setText("("+checkCount+"/"+stuCount+")");
            viewHolderGroup.class_name_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (!compoundButton.isPressed()){
                        return;
                    }
                    if (b){
                        studentBeans.get(i).setCheckCount(studentBeans.get(i).getList().size());
                        setGroupCheckStatus(i,studentBeans.get(i).getList(),1);
                        if (mContext instanceof ChooseGroupActivity){
                            ((ChooseGroupActivity)mContext).setCheckCountAndCheckStatus(studentBeans);
                        }
                        ChooseGroupAdapter.this.notifyDataSetChanged();
                    } else {
                        studentBeans.get(i).setCheckCount(0);
                        setGroupCheckStatus(i,studentBeans.get(i).getList(),2);
                        if (mContext instanceof ChooseGroupActivity){
                            ((ChooseGroupActivity)mContext).setCheckCountAndCheckStatus(studentBeans);
                        }
                        ChooseGroupAdapter.this.notifyDataSetChanged();
                    }
                }
            });

        }
        if (b){
            viewHolderGroup.class_name_go.setImageResource(R.mipmap.icon_down);
        }else {
            viewHolderGroup.class_name_go.setImageResource(R.mipmap.choose_group_put_away);
        }
        return view;
    }

    @Override
    public View getChildView(final int i,final int i1, boolean b, View view, ViewGroup viewGroup) {
        final ViewHolderChild viewHolderChild;
        if (null == view){
            viewHolderChild = new ViewHolderChild();
            view = View.inflate(mContext,R.layout.activity_choose_group_child,null);
            viewHolderChild.stu_name_check =  view.findViewById(R.id.stu_name_check);
            viewHolderChild.stu_name_text =  view.findViewById(R.id.stu_name_text);
            view.setTag(viewHolderChild);
        }else {
            viewHolderChild = (ViewHolderChild) view.getTag();
        }
        if (null != studentBeans){
            final int checkCount = studentBeans.get(i).getCheckCount();
            final int stuCount = studentBeans.get(i).getList().size();
            viewHolderChild.stu_name_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (!compoundButton.isPressed()){
                        return;
                    }
                    if (b){
                        studentBeans.get(i).getList().get(i1).setCheckStatus(1);
                        studentBeans.get(i).setCheckCount(checkCount+1);
                        if (studentBeans.get(i).getList().get(i1).getIsVip() == 1){
                            setChildCheckStatus(i,studentBeans.get(i).getList().get(i1),1);
                        }
                        if (mContext instanceof ChooseGroupActivity){
                            ((ChooseGroupActivity)mContext).setCheckCountAndCheckStatus(studentBeans);
                        }
                        ChooseGroupAdapter.this.notifyDataSetChanged();
                    }else {
                        studentBeans.get(i).getList().get(i1).setCheckStatus(2);
                        studentBeans.get(i).setCheckCount(checkCount-1);
                        if (studentBeans.get(i).getList().get(i1).getIsVip() == 1){
                            setChildCheckStatus(i,studentBeans.get(i).getList().get(i1),2);
                        }
                        if (mContext instanceof ChooseGroupActivity){
                            ((ChooseGroupActivity)mContext).setCheckCountAndCheckStatus(studentBeans);
                        }
                        ChooseGroupAdapter.this.notifyDataSetChanged();
                    }
                }
            });

            if (checkCount == stuCount){
                viewHolderChild.stu_name_check.setChecked(true);
                viewHolderChild.stu_name_check.setSelected(true);
            } else if (checkCount == 0){
                viewHolderChild.stu_name_check.setChecked(false);
                viewHolderChild.stu_name_check.setSelected(true);
            }else if (1 == studentBeans.get(i).getList().get(i1).getCheckStatus()){
                viewHolderChild.stu_name_check.setChecked(true);
                viewHolderChild.stu_name_check.setSelected(true);
            }else if (2 == studentBeans.get(i).getList().get(i1).getCheckStatus()){
                viewHolderChild.stu_name_check.setChecked(false);
                viewHolderChild.stu_name_check.setSelected(true);
            }
            viewHolderChild.stu_name_text.setText(studentBeans.get(i).getList().get(i1).getUserName());
        }
        return view;
    }


    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    private void setChildCheckStatus(int postion,StudentBean studentBean,int checkStatus){
        if (postion != studentBeans.size() - 1){
            StudentGroupBean studentVipGroup = studentBeans.get(studentBeans.size() - 1);
            if (checkStatus == 1){
                studentVipGroup.setCheckCount(studentVipGroup.getCheckCount() + 1);
            }else {
                studentVipGroup.setCheckCount(studentVipGroup.getCheckCount() - 1);
            }
            //遍历vip小组 设置选中状态
            List<StudentBean> viplist = studentVipGroup.getList();
                for (int a = 0; a < viplist.size(); a++) {
                    if (studentBean.getUserId() == viplist.get(a).getUserId()){
                        viplist.get(a).setCheckStatus(checkStatus);
                        break;
                }
            }
        }else {
            for (int a = 0; a < studentBeans.size()-1; a++) {
                List<StudentBean> list = studentBeans.get(a).getList();
                for (int b = 0; b < list.size(); b++) {
                        if (studentBean.getUserId() == list.get(b).getUserId()) {
                            list.get(b).setCheckStatus(checkStatus);
                            if (checkStatus == 1){
                                studentBeans.get(a).setCheckCount(studentBeans.get(a).getCheckCount() + 1);
                            }else {
                                studentBeans.get(a).setCheckCount(studentBeans.get(a).getCheckCount() - 1);
                            }
                            break;
                        }
                    }
                }
        }
    }


    private void setGroupCheckStatus(int postion, List<StudentBean> studentBeanList,int checkStatus){
        List<Integer> vipUserList = new ArrayList<>();
        if (postion != studentBeans.size()-1){
            for (int i = 0; i < studentBeanList.size(); i++) {
                //取出该小组中所有vip用户
                if (checkStatus == 1){
                    if (studentBeanList.get(i).getIsVip()==1 && studentBeanList.get(i).getCheckStatus() != 1){
                        vipUserList.add(studentBeanList.get(i).getUserId());
                    }
                }else {
                    if (studentBeanList.get(i).getIsVip()==1 && studentBeanList.get(i).getCheckStatus() == 1){
                        vipUserList.add(studentBeanList.get(i).getUserId());
                    }
                }
                studentBeanList.get(i).setCheckStatus(checkStatus);

            }
            StudentGroupBean studentVipGroup = studentBeans.get(studentBeans.size() - 1);
            if (checkStatus == 1){
                studentVipGroup.setCheckCount(studentVipGroup.getCheckCount() + vipUserList.size());
            }else {
                studentVipGroup.setCheckCount(studentVipGroup.getCheckCount() - vipUserList.size());
            }
            //遍历vip小组 设置选中状态
            List<StudentBean> viplist = studentVipGroup.getList();
            for (int i = 0; i < vipUserList.size(); i++) {
                for (int a = 0; a < viplist.size(); a++) {
                    if (vipUserList.get(i) == viplist.get(a).getUserId()){
                        viplist.get(a).setCheckStatus(checkStatus);
                        continue;
                    }
                }
            }
        }else {
            for (int i = 0; i < studentBeanList.size(); i++) {
                if (checkStatus == 1){
                    if (studentBeanList.get(i).getIsVip()==1 && studentBeanList.get(i).getCheckStatus() != 1){
                        vipUserList.add(studentBeanList.get(i).getUserId());
                    }
                }else {
                    if (studentBeanList.get(i).getIsVip()==1 && studentBeanList.get(i).getCheckStatus() == 1){
                        vipUserList.add(studentBeanList.get(i).getUserId());
                    }
                }
                studentBeanList.get(i).setCheckStatus(checkStatus);

            }
            //遍历集合 设置不同小组中的vip选中状态
                for (int a = 0; a < studentBeans.size()-1; a++) {
                    List<StudentBean> list = studentBeans.get(a).getList();
                    int count = 0;
                    for (int b = 0; b < list.size(); b++) {
                        for (int i = 0; i < vipUserList.size(); i++) {
                            if (vipUserList.get(i) == list.get(b).getUserId()) {
                                list.get(b).setCheckStatus(checkStatus);
                                count++;
                                continue;
                            }
                        }
                    }
                    if (count != 0){
                        if (checkStatus == 1){
                            studentBeans.get(a).setCheckCount(studentBeans.get(a).getCheckCount() + count);
                        }else {
                            studentBeans.get(a).setCheckCount(studentBeans.get(a).getCheckCount() - count);
                        }
                    }
            }

        }
    }

    private static class ViewHolderGroup{
        private CheckBox class_name_check;
        private TextView class_name_text;
        private TextView class_count_text;
        private ImageView class_name_go;
    }

    private static class ViewHolderChild{
        private CheckBox stu_name_check;
        private TextView stu_name_text;
    }

    public void setNewData(List<StudentGroupBean> studentBeans){
        this.studentBeans = studentBeans;
        this.notifyDataSetChanged();
    }
}
